﻿using System;
using System.Text;
using System.Globalization;
using System.Security.Cryptography;

namespace PKSpeed
{
    public class Encrypt
    {
        /// <summary>
        /// 获取加密随机key
        /// </summary>
        /// <returns></returns>
        //public string GenerateKey()
        //{
        //    DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
        //    return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
        //}

            /// <summary>
            /// md5加密
            /// </summary>
            /// <param name="sInputString">输入的字符串</param>
            /// <param name="sKey">加密的key</param>
            /// <returns>加密后的字符串</returns>
            public static string EncryptString(string sInputString, string sKey)
            {
                byte[] data = Encoding.UTF8.GetBytes(sInputString);
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                ICryptoTransform desencrypt = DES.CreateEncryptor();
                byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
                return BitConverter.ToString(result);
            }


            /// <summary>
            /// md5解密
            /// </summary>
            /// <param name="sInputString">输入字符串</param>
            /// <param name="sKey">解密的key</param>
            /// <returns>解密后字符串</returns>
            //public string DecryptString(string sInputString, string sKey)
            //{
            //    string[] sInput = sInputString.Split("-".ToCharArray());
            //    byte[] data = new byte[sInput.Length];
            //    for (int i = 0; i < sInput.Length; i++)
            //    {
            //        data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
            //    }
            //    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            //    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            //    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            //    ICryptoTransform desencrypt = DES.CreateDecryptor();
            //    byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
            //    return Encoding.UTF8.GetString(result);
            //}
        }
}
